<template>
  <el-dialog title="选择应用" :visible.sync="visible" width="900px" top="5vh" append-to-body>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item prop="id">
        <el-select
          v-model="queryParams.id"
          placeholder="请选择应用分类"
          style="width: 240px"
          @change="handleTypeChange"
        >
          <el-option
            v-for="(type, i) in filteredTypeList"
            :key="type.categoryId"
            :label="type.categoryName"
            :value="type.categoryId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table @row-click="clickRow" :data="appList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="图片" align="center" prop="dictId">
        <template slot-scope="scope">
          <img :src="getImg(scope.row.icon)" alt="" width="60" height="60">
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" :show-overflow-tooltip="true"/>

      <el-table-column label="应用小类" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.application_small_type" :value="scope.row.dictDataId"/>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectApp">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {getTypeList, authAppSelect, unSelectApp} from "@/api/system/application.js"
  export default {
    name: "selectApp",
    dicts: ['application_small_type'],
    props: {
      // 角色编号
      roleId: {
        type: [Number, String]
      }
    },
    data() {
      return {
        visible: false,
        // 遮罩层
        loading: true,
        // 选中应用组
        appIds: [],
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          roleId: undefined,
          categoryId: undefined
        },
        typeList: [],
        appList: [],
        total: 0
      };
    },

    methods: {

      show() {
        this.queryParams.roleId = this.roleId;
        this.getCategoryList()
        this.getList()
        this.visible = true;

      },

      clickRow(row) {
        this.$refs.table.toggleRowSelection(row);
      },

      //获取分类列表
      getCategoryList() {
        getTypeList().then(res => {
          if (res.code === 200) {
            if (res.data.length > 0) {
              this.typeList = res.data
            }
          } else {
            this.$message.error(res.msg)
          }
        }).catch(err => {
          this.$message.error(err)
        })
      },

      //获取应用列表
      getList() {
        unSelectApp(this.queryParams).then(res => {
          if (res.code === 200) {
            this.loading = false
            this.appList = res.rows
            this.total = res.total
          } else {
            this.$message.error(res.msg)
          }
        }).catch(err => {
          this.$message.error(err)
        })
      },

      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },

      handleSelectionChange(selection) {
        this.appIds = selection.map(item => item.applicationInfoId)
        this.multiple = !selection.length
      },

      //切换分类
      handleTypeChange(e) {
        this.queryParams.categoryId = e
        this.queryParams.pageNum = 1
        this.getList()
      },

      //获取图片
      getImg(path) {
        let url = process.env.VUE_APP_BASE_API + path
        return url
      },

      //提交
      handleSelectApp() {
        const roleId = this.queryParams.roleId;
        const appIds = this.appIds.join(",");
        if (appIds == "") {
          this.$modal.msgError("请选择要分配的用户");
          return;
        }
        authAppSelect({ roleId: roleId, applicationIds: this.appIds }).then(res => {
          this.$modal.msgSuccess(res.msg);
          if (res.code === 200) {
            this.visible = false;
            this.$emit("ok");
          }
        });
      }
    },
    computed: {
      filteredTypeList() {
        return this.typeList.filter(type => type.categoryId !== 22);
      }
    }
  }
</script>

<style scoped>

</style>
